package com.wolfalpha.utron.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;

import com.wolfalpha.utron.R;
import com.wolfalpha.utron.activity.base.BaseNormalActivity;
import com.wolfalpha.utron.adapter.ItemInformationSimpleAdapter;
import com.wolfalpha.utron.app.AppConfig;
import com.wolfalpha.utron.bean.ItemInformation;
import com.wolfalpha.utron.service.UserService;
import com.wolfalpha.utron.service.impl.UserServiceImpl;
import com.wolfalpha.utron.ui.UIHelper;
import com.wolfalpha.utron.utils.ThreadPoolUtils;
import com.wolfalpha.utron.view.XListView;

import java.util.ArrayList;
import java.util.List;

/**
 * 收藏
 * <p/>
 * Created by luodon on 15/8/19.
 */
public class MyCollectionActivity extends BaseNormalActivity {

    private XListView lvCollection;      //列表控件

    private ItemInformationSimpleAdapter itemInformationAdapter;//资讯适配器
    private List<ItemInformation> informations = new ArrayList<ItemInformation>();

    private UserService userService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collection);
        userService = new UserServiceImpl();

        intiView();
        loadData();
    }

    private int page = 1;

    /**
     * 加载列表的数据
     */
    private void loadData() {
        ThreadPoolUtils.execute(new Runnable() {
            @Override
            public void run() {
                informations = userService.getMyCollections(1);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (informations != null) {
                            itemInformationAdapter.setData(informations);
                            if (informations.size() < AppConfig.LIST_PAGE_SIZE) {
                                lvCollection.setPullLoadEnable(false);
                            } else {
                                lvCollection.setPullLoadEnable(true);
                            }
                        } else {
                            lvCollection.setPullLoadEnable(false);
                        }
                    }
                });

            }
        });

    }

    /**
     * 初始化界面
     */
    private void intiView() {
        setTvTitle("我的收藏");

        lvCollection = (XListView) findViewById(R.id.lv_collection);
        itemInformationAdapter = new ItemInformationSimpleAdapter(this, informations);
        lvCollection.setPullLoadEnable(false);

        lvCollection.setAdapter(itemInformationAdapter);
        lvCollection.setXListViewListener(new XListView.IXListViewListener() {
                                              @Override
                                              public void onRefresh() {
                                                  page = 1;
                                                  ThreadPoolUtils.execute(new Runnable() {
                                                                              @Override
                                                                              public void run() {
                                                                                  final List<ItemInformation> informations1 = userService.getMyCollections(page);

                                                                                  informations = informations1;
                                                                                  runOnUiThread(new Runnable() {
                                                                                      @Override
                                                                                      public void run() {
                                                                                          if (informations1 != null) {
                                                                                              informations = informations1;
                                                                                              itemInformationAdapter.setData(informations);
                                                                                              if (informations.size() < AppConfig.LIST_PAGE_SIZE) {
                                                                                                  lvCollection.setPullLoadEnable(false);
                                                                                              } else {
                                                                                                  lvCollection.setPullLoadEnable(true);
                                                                                              }
                                                                                          } else {
                                                                                              lvCollection.setPullLoadEnable(false);
                                                                                          }
                                                                                          lvCollection.stopRefresh();
                                                                                      }
                                                                                  });

                                                                              }
                                                                          }

                                                  );
                                              }

                                              @Override
                                              public void onLoadMore() {
                                                  ThreadPoolUtils.execute(new Runnable() {
                                                      @Override
                                                      public void run() {
                                                          final List<ItemInformation> informations1 = userService.getMyCollections(++page);

                                                          runOnUiThread(new Runnable() {
                                                              @Override
                                                              public void run() {
                                                                  if (informations1 != null) {
                                                                      informations.addAll(informations1);
                                                                      itemInformationAdapter.setData(informations);
                                                                      if (informations.size() < AppConfig.LIST_PAGE_SIZE) {
                                                                          lvCollection.setPullLoadEnable(false);
                                                                      } else {
                                                                          lvCollection.setPullLoadEnable(true);
                                                                      }
                                                                  } else {
                                                                      page--;
                                                                      lvCollection.setPullLoadEnable(false);
                                                                  }
                                                                  lvCollection.stopRefresh();
                                                              }
                                                          });

                                                      }
                                                  });
                                              }
                                          }

        );

        lvCollection.setOnItemClickListener(new AdapterView.OnItemClickListener()

        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position < 1 || position > informations.size()) {
                    //过滤刷新头部
                    return;
                } else {
                    Bundle bundle = new Bundle();
                    bundle.putInt("infoId", informations.get(position - 1).getId());
                    UIHelper.showActivity(MyCollectionActivity.this, InformationDetailActivity.class, bundle);
                }
            }
        });
    }

}
